// pages/signi/signi.js
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
const DrainageScene = require('../../../utils/drainage_scene.js')

var app = getApp();
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    minDate: new Date().getTime(),
    maxDate: new Date().getTime(),
    defaultDate: new Date(),
    defaultDates:new Date(),
    monthCont: 0,
    yearCont:0,
    ///////////////////////////////
    param: null,
    clockNum: 3,
    now:'',//显示时间
    sign_status: false, // 签到状态
    sign_total: '', // 累计签到天数 
    continue_days: '', // 连续签到天数 
    cumulative: '', // 累计奖励
    sign_log: [], // 签到天数
    sign_name: '', // 签到名字
    sharedata: {},
    showYear:    new Date().getFullYear(),
    showMonth:new Date().getMonth(),
    date: '',
    show: false,
    user_name:'',
    uid:'',//用户ID
    avatar_image:'',//头像地址
    show_list:[],//签到奖励列表
    signedList:[],
    // 日期的初始数据
    // year: '',
    // month: '',
    m: '',
    w: '',
    src: '',
    signlog: '',
    isshow: '', // 判断是否签到
    obj: [], // 存值
    tapnow:true,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getData()
    this.getSign();
    // 日历
    // 初始化年份和月数
    this.getData(1);
    this.getnewDate();


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  signnow(e) {
    var that = this;
    if(!that.data.tapnow){
      console.log('点击限制！！！');
      return;
    }
    if (this.data.sign_status) {
      wx.showToast({
        title: '今日已经' + this.data.sign_name,
        icon: 'none'
      });
    } else {
      let isBindDrainageScene = DrainageScene.bindDrainageScene({
        scene: 4,
        title: `${this.data.sign_name}`
      });
      if(!isBindDrainageScene) return;//爆客码绑定场景

      that.setData({
        tapnow : false
      });
      let urlStr = app.getNetAddresss('plugin.sign.Frontend.Modules.Sign.Controllers.sign.sign');
      app._getNetWork({
        url: urlStr,
        success: function (res) {
          if (res.data.result == 1) {
            wx.showLoading({
              title: '加载中',
              mask: true
            });
            that.selectComponent("#calendar").getSign();
            that.getSign();
            that.getnewDate();
            wx.navigateTo({
              url: '/packageA/member/signrecord/signrecord'
            });
          } else if (res.data.result == 0) {
            wx.showToast({
              icon: 'none',
              title: res.data.msg,
              duration: 1500
            });

            setTimeout(function() {
              that.setData({
                tapnow : true
              });
            }, 1500); //延迟时间
          }
        }
      });
    }
  },
  initShare() {
    let that = this;
    let urlStr = app.getNetAddresss('member.member.wxJsSdkConfig');
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: _url,
      success: function (resdata) {
        var res = resdata;
        if (res.result == 1) {
          that.share(response.data);
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  getSign() {
    var that = this;
    let urlStr = app.getNetAddresss('plugin.sign.Frontend.Modules.Sign.Controllers.sign-log.index');
    app._getNetWork({
      url: urlStr,
      success: function (res) {
        if (res.data.result == 1) {
          wx.hideLoading();
          wx.setStorage({
            key: 'sign_log',
            data: res.data.data.sign_log
          });
          that.setData({
            sign_status: res.data.data.sign_status,
            sign_total: res.data.data.sign_total,
            continue_days: res.data.data.continue_days,
            cumulative: res.data.data.cumulative,
            sign_log: res.data.data.sign_log,
            sign_name: res.data.data.sign_name,
            signedList: res.data.data.sign_log,
            user_name:res.data.data.member.username,
            uid:res.data.data.member.uid,
            avatar_image:res.data.data.member.avatar_image,
            show_list:res.data.data.show_list,
            // themeBaseColor:res.data.basic_info.theme_color
          });
          //设置导航栏背景颜色
          wx.setNavigationBarColor({
            frontColor: '#ffffff',
            backgroundColor: that.data.themeBaseColor,
            animation: {
              duration: 400,
              timingFunc: 'easeIn'
            }
          });
          wx.setNavigationBarTitle({
            title:that.data.sign_name
          })
          wx.setStorage({
            key: 'cumulative',
            data: res.data.data.cumulative
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          });
        }
      }
    });
  },
  // 获取时间
  // getData() {
  //   let that = this;
  //   let urlStr = app.getNetAddresss('plugin.sign.Frontend.Controllers.share.index');
  //   app._getNetWork({
  //     url: urlStr,
  //     success: function (res) {
  //       if (res.result == 1) {
  //         that.sharedata = res.data.share;
  //         that.initShare();
  //       }
  //     },
  //     fail: function (res) {
  //       console.log(res.msg);
  //     }
  //   });
  // },
  // 跳转到签到记录
  gosignInfo() {
    wx.navigateTo({
      url: '/packageA/member/signrecord/signrecord'
    });
  },
  // 右侧标题被点击时
  onClickRight() {
    wx.navigateTo({
      url: '/packageA/member/signrules/signrules'
    });
  },
  // 日历
  getnewDate() {
    var that = this;
    let d = new Date(that.data.year, that.data.month, 0);
    that.setData({
      m: d.getDate()
    });
    // 吧天数放进数组对象中
    let m = d.getDate();
    let obj = [];
    for (let i = 1; i <= m; i++) {
      obj.push({
        day: i
      });
    }
    this.setData({
      obj});
    let data = {
      month: that.data.month
    };
    that.getsigninfo(data);

    // that.data.m = d.getDate()
    let wx = new Date(that.data.year, that.data.month - 1, 1);
    let w1 = wx.getDay();
    // that.data.w = w1 * 14.27
    that.setData({
      w: w1 * 14.27
    });
  },
  put() {
    var that = this;
    var month = that.data.month;
    if (month <= 1) {
      that.setData({
        year: that.data.year - 1,
        month: 12
      });
    } else {
      var month1 = that.data.month;
      that.setData({
        month: month1 - 1
      });
    }
    // that.getData(1)
    // ==============================================
    let data = {
      month: that.data.month
    };
    that.getsigninfo(data);
    that.getnewDate();
  },
  getsigninfo(data) {
    // 请求签到日期的数组
    var that = this;
    let urlStr = app.getNetAddresss('plugin.sign.Frontend.Modules.Sign.Controllers.sign-log.index');
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: data,
      success: function (res) {
        that.setData({
          signlog: res.data.data.sign_log
        });
        let obj = that.data.obj;
        
        var nowyear = new Date().getFullYear();
        let signArr = res.data.data.sign_log;
        for (let j = 0; j < obj.length; j++) {
          for (let o = 0; o < signArr.length; o++) {
            if ((obj[j].day-1) == signArr[o] && that.data.year == nowyear) {
              obj[j].bool = true;
            }
          }
        }
        that.setData({
          obj});
      }
    });
  },
  add() {
    var that = this;
    let month = that.data.month;
    if (that.data.month >= 12) {
      let yea = that.data.year;
      that.setData({
        year: yea + 1,
        month: 1
      });
    } else {
      var mon = month;
      that.setData({
        month: mon + 1
      });
    }
    that.getnewDate();
  },
  getData(type) {
    var date = new Date();
    var hengGang = "-";
    var maoHao = ":";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var curDate = date.getDate();
    var curHours = date.getHours();
    var curMinutes = date.getMinutes();
    var curSeconds = date.getSeconds();

    if (month >= 1 && month <= 9) {
      month = "0" + month;
    }
    if (curDate >= 0 && curDate <= 9) {
      curDate = "0" + curDate;
    }
    if (curHours >= 0 && curHours <= 9) {
      curHours = "0" + curHours;
    }
    if (curMinutes >= 0 && curMinutes <= 9) {
      curMinutes = "0" + curMinutes;
    }
    if (curSeconds >= 0 && curSeconds <= 9) {
      curSeconds = "0" + curSeconds;
    }
    var currentdate = "";
    if (type == "year") {
      currentdate = year;
      return currentdate;
    } else if (type == "month") {
      currentdate = year + hengGang + month;
      return currentdate;
    } else {
      currentdate = year + hengGang + month + hengGang + curDate + " " + curHours + ' ' + maoHao + ' ' + curMinutes +  ' ' + maoHao + ' ' + curSeconds;
      var now = this.data.now  
      now = currentdate
        this.setData({
          now
        })
    }
  },
  onConfirm(event) {
    this.setData({
      show: false,
      date: this.formatDate(event.detail),
    });
  },
  onClose() {
    this.setData({ show: false });
  },
  // 切换月 设置已签到的日期
  handleChangeMonth(event) {
  	// 传过来的日期格式为 2022/6
    let yearMonth = event.detail.date


  },
  change(){
    const child = this.selectComponent('.calendar')
    child.setData({
      todayStyle:'is-signed'
    })
    this.setData({
      signedList:['2022/9/1','2022/9/20','2022/9/21','2022/9/22','2022/8/8','2022/9/27']
    })
  },
});
